package com.guochaojava.api;

import cn.hutool.core.util.NumberUtil;
import com.github.pagehelper.PageInfo;
import com.guochaojava.model.Member;
import com.guochaojava.model.Record;
import com.guochaojava.service.MemberService;
import com.guochaojava.service.RecordDetailService;
import com.guochaojava.service.RecordService;
import com.guochaojava.util.Result;
import com.guochaojava.view.AchievementView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 我的相关接口
 *
 * @author guochao
 * @since 1.0.0
 */
@Api(tags = "我的相关接口")
@Validated
@RestController
@RequestMapping("/my")
public class MyApi {

    @Autowired
    private MemberService memberService;
    @Autowired
    private RecordService recordService;
    @Autowired
    private RecordDetailService recordDetailService;


    @ApiOperation(value = "我的信息")
    @PostMapping("/info")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openId", value = "用户openId", dataType = "string", paramType = "query")
    })
    public Object info(@NotNull String openId) {
        try {
            Member member = memberService.selectByOpenId(openId);
            return Result.buildOK(member);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.buildError();
        }
    }

    @ApiOperation(value = "我的消息")
    @PostMapping("/message")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", dataType = "int", paramType = "query")
    })
    public Object message(@NotNull Integer id) {
        try {
            List<Record> list = recordService.selectByRivalId(id);
        /*    List<Record> view = new ArrayList<Record>();
            for (Record record : list) {
                view.add(record);
            }*/
            return Result.buildOK(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.buildError();
        }
    }

    @ApiOperation(value = "阅读信息")
    @PostMapping("/read")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "记录id", dataType = "int", paramType = "query")
    })
    public Object read(@NotNull Integer id) {
        try {
            Record record = recordService.read(id);
            return Result.buildOK(record);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.buildError();
        }
    }

    @ApiOperation(value = "我的成就")
    @PostMapping("/achievement")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "openId", value = "用户openId", dataType = "string", paramType = "query")
    })
    public Object achievement(@NotNull Integer id, @NotNull String openId) {
        try {
            AchievementView view = new AchievementView();
            int total = recordDetailService.totalById(id);
            view.setWins(total);
            int iniWin = recordDetailService.iniWinByMemberId(id);
            int accWin = recordDetailService.accWinByMemberId(id);
            if (total != 0) {
                view.setRate(String.valueOf(NumberUtil.round(NumberUtil.mul(NumberUtil.div(iniWin + accWin, total), 100), 1)));
            }

            int rank = memberService.rank(id);
            view.setRank(rank);

            Member member = memberService.selectByOpenId(openId);
            view.setName(member.getNickName());
            view.setImg(member.getImg());
            //是否还有未读消息
            boolean read = recordService.readFlag(id);
            view.setRead(read);
            return Result.buildOK(view);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.buildError();
        }
    }

    @ApiOperation(value = "我的比赛记录")
    @PostMapping("/history")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", dataType = "int", defaultValue = "10", paramType = "query")
    })
    public Object history(@NotNull Integer id, @NotNull Integer pageNum, @NotNull Integer pageSize) {
        try {
            PageInfo<Record> list = recordService.myRecord(id,pageNum,pageSize);
            return Result.buildOK(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.buildError();
        }
    }

    @ApiOperation(value = "比赛详情(对手看)")
    @PostMapping("/history/detail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "比赛记录id", dataType = "int", paramType = "query")
    })
    public Object info(@NotNull Integer id) {
        try {
            Record record = recordService.selectRivalDetailById(id);
            return Result.buildOK(record);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.buildError();
        }
    }
}